#!/usr/bin/python
# vim:fileencoding=utf8

import os

from gnTai import io
from gnTai.aiexception import AIException
from gnTai.words.article import Article
from gnTai.words.noun import Noun
from gnTai.words.verb import Verb
from gnTai.words.specialverb import to_be as _to_be
from gnTai.words.specialverb import to_do as _to_do

NAME_2_CLASS = {'noun':Noun, 'verb':Verb, 'article':Article}

class EnglishKnowledge:
    __ARTICLE_DICTIONARY = {'a':Article, 'an':Article, 'the':Article}
    __verb_dictionary = {'be': _to_be, 'am':_to_be, 'are':_to_be, 'is':_to_be,
                     'do': _to_do, 'does':_to_do
                     }
    to_do = _to_do
    to_be = _to_be
    def __init__(self, words_file_name):
        self.article_dictionary = self.__ARTICLE_DICTIONARY
        self.noun_dictionary = {}
        self.verb_dictionary = EnglishKnowledge.__verb_dictionary
        self.dictionaries = {
                             'noun':self.noun_dictionary,
                             'verb':self.verb_dictionary,
                             'article':self.article_dictionary
                             }
        io.load_dictionary(words_file_name, self.dictionaries)
        
    def is_part_of_speech(self, word, part_of_speech):
        return word in self.dictionaries[part_of_speech]
    
    def get_part_of_speech(self, word, part_of_speech):
        if self.is_part_of_speech( word, part_of_speech):
            return self.dictionaries[part_of_speech][word]

    def get_part_of_speech_(self, word):
        res = {}
        for part_of_speech, Class in NAME_2_CLASS.items():
             cur = [
                   (self.dictionaries[part_of_speech][base], form)
                   for base, form in Class.base_form(word)
                   if base in self.dictionaries[part_of_speech]
                   ]
             
             if len(cur) > 0:
                  res[part_of_speech] = cur
        
        return res



try:
    test_knowledge = EnglishKnowledge(WORDS_FILE_NAME)
except:
    test_knowledge = None     
